<?php


namespace Trawind\Remotes;


use Trawind\Helpers\HttpClientHelper;

trait RemoteEnhance
{


    public $baseRoute = '';


    public function buildPromiseOptions(array $ids)
    {
        return $this->buildPromiseOptionsByField($ids);
    }

    public function buildPromiseOptionsByField(array $values, $field = 'id')
    {
        $values = array_filter(array_unique($values));
        $option = [
            'base_uri' => $this->domain,
            'query' => [
                'search' => $field.':'.join(',', empty($values) ? [0] : $values),
                'searchFields' => $field.':in',
                'pageSize' => 0,
                'source' => 'remote'
            ]
        ];

        HttpClientHelper::requestLog([
            'to_url' => $this->domain.$this->baseRoute,
            'option' => $option,
            'current_url' => '('.request()->method().')'.urldecode(url()->full())
        ]);
        return $option;
    }


    /**
     * Must implement `all` method
     *
     * @param  array  $ids
     * @param  bool   $isColumnData
     * @return array|mixed
     */
    public function getListById(array $ids, bool $isColumnData = true)
    {
        if (empty($ids = array_filter(array_unique($ids)))) {
            return [];
        }

        $result = $this->getListBySearch([
            'search' => 'id:'.join(',', $ids),
            'searchFields' => 'id:in',
            'pageSize' => 0,
        ]);

        return $isColumnData ? array_column($result['data'] ?? [], null, 'id') : $result;
    }

    /**
     * @param  array   $indexList
     * @param  string  $selectField
     * @param  string  $selectValueField
     * @param  bool    $isColumnData
     * @return array
     */
    public function getListBySelectField(
        array $indexList,
        $selectField = 'id',
        $selectValueField = '',
        bool $isColumnData = true
    ) {
        $result = $this->getListBySearch([
            'search' => $selectField.':'.implode(',', $indexList),
            'searchFields' => $selectField.':in',
            'filter' => $selectValueField ? $selectField.';'.$selectValueField : '',
            'pageSize' => 0,
        ]);
        return $isColumnData ? array_column($result['data'] ?? [], null, 'id') : $result;
    }

    /**
     * Must implement `all` method
     *
     * @param  array  $search
     * @return mixed
     */
    public function getListBySearch(array $search, bool $isRemote = true)
    {
        return $this->setClientOptions(['query' => $search], $isRemote)->all();
    }

    /**
     * @param         $id
     * @param  array  $search
     * @return mixed
     */
    public function getDetailBySearch($id, array $search, bool $isRemote = true)
    {
        return $this->setClientOptions(['query' => $search], $isRemote)->find($id);
    }

    /**
     * @param          $list
     * @param  array   $searchFieldsList
     * @param  string  $selectValueField
     * @return mixed
     */
    public function getListBySelectFields(
        $list,
        $searchFieldsList = [],
        $selectValueField = ''
    ) {
        $search = '';
        $searchFields = '';
        foreach ($list as $key => $value) {
            if (is_array($value)) {
                $search .= $key.':'.join(',', $value).';';
                $searchFields .= $key.':in;';
            } else {
                $search .= $key.':'.$value.';';
                $searchFields .= $key.':=;';
            }
        }
        $search = trim($search, ';');
        $searchFields = trim($searchFields, ';');
        $result = $this->getListBySearch([
            'search' => $search,
            'searchFields' => $searchFields,
            'filter' => $selectValueField,
            'searchJoin' => 'and',
            'pageSize' => 0,
        ]);
        return isset($result['data']) ? $result['data'] : $result;
    }

}